package com.kevin.miniospring;

import com.kevin.minio.config.MinioConfiguration;
import com.kevin.minio.util.FileUploadClientFactory;
import com.kevin.minio.util.FileUploadClientFactoryBuilder;
import lombok.Setter;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

/**
 * @author Kevin Liu
 * @date 2020/6/10 8:35 上午
 */
@Setter
public class FileUploadTemplate implements FactoryBean<FileUploadClientFactory> , InitializingBean {

    private MinioConfiguration configuration;

    private FileUploadClientFactory fileUploadClientFactory;


    @Override
    public FileUploadClientFactory getObject() throws Exception {
        if(fileUploadClientFactory == null) {
            afterPropertiesSet();
        }
        return this.fileUploadClientFactory;
    }

    @Override
    public Class<? extends FileUploadClientFactory> getObjectType() {
        return this.fileUploadClientFactory == null ? FileUploadClientFactory.class : this.fileUploadClientFactory.getClass();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Assert.notNull(this.configuration,"Property 'configuration' is required");
        this.fileUploadClientFactory = FileUploadClientFactoryBuilder.build(this.configuration);
    }
}
